package com.ship.dispatch.service.fob.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ship.common.core.utils.DateUtil;
import com.ship.common.core.utils.HttpRequestUtil;
import com.ship.dispatch.bean.SpArrangeSchemeDetail;
import com.ship.dispatch.bean.SpDispatchSchemeDetail;
import com.ship.dispatch.bean.SpDispatchSchemeExecute;
import com.ship.dispatch.bean.boats.ShipDashboarDataRq;
import com.ship.dispatch.bean.fob.*;
import com.ship.dispatch.bean.orderPay.ConThirdPartyMeasurement;
import com.ship.dispatch.bo.shipBusiness.ThirdPartyMeasureBo;
import com.ship.dispatch.bo.shipBusiness.ThirdPartyMeasureRecordBo;
import com.ship.dispatch.bpm.service.TaskService;
import com.ship.dispatch.bpm.utils.CalculateDateUtil;
import com.ship.dispatch.bpm.utils.JsonUtils;
import com.ship.dispatch.bpm.vo.ManageEntityVo;
import com.ship.dispatch.mapper.fob.SpFobQrxzgMapper;
import com.ship.dispatch.mapper.fob.SpFobSyzlxzwjMapper;
import com.ship.dispatch.mapper.fob.SpFobSyzlzzwjMapper;
import com.ship.dispatch.mapper.orderPay.ConThirdPartyMeasurementMapper;
import com.ship.dispatch.service.*;
import com.ship.dispatch.service.fob.*;
import com.ship.dispatch.service.shipBusiness.SmThirdPartyMeasureService;
import com.ship.dispatch.util.PropertiesUtil;
import com.ship.dispatch.util.StringUtils;
import com.ship.dispatch.vo.shipReport.SynchronThirdPartyMeasureVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yl
 * @title SpFobSyzlxzwjServiceImpl
 * @description 审阅并整理卸载文件
 * @create 2023/12/18 0018
 */
@Service
public class SpFobSyzlxzwjServiceImpl extends FobBaseServiceImpl<SpFobSyzlxzwjMapper, SpFobSyzlxzwj> implements SpFobSyzlxzwjService {

    @Autowired(required = false)
    SpFobSyzlxzwjMapper mapperapper;

    @Autowired
    SpFobFileRecordService spFobFileRecordService;

    @Autowired
    SpDispatchSchemeExecuteService schemeDetailService;
    @Autowired
    SpArrangeSchemeDetailService spArrangeSchemeDetailService;

    @Autowired
    @Lazy
    private SpDispatchSchemeDetailService spDispatchSchemeDetailService;
    @Autowired
    private TaskService taskService;

    private final AsyncService asyncService;

    public SpFobSyzlxzwjServiceImpl(AsyncService asyncService) {
        this.asyncService = asyncService;
    }
    @Override
    public void updateEntity(ManageEntityVo manageEntityVo, String json) {
        SpFobSyzlxzwj object = JsonUtils.jsonToObject(json,SpFobSyzlxzwj.class);
        SpFobSyzlxzwj his = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlxzwj>().eq(SpFobSyzlxzwj::getProceeInstanceId,manageEntityVo.getProcessInstanceId()).ne(SpFobSyzlxzwj::getStatus,-2));
        if(manageEntityVo.getOperationType()==1) {
            object.setStatus(1);
        }
        if(manageEntityVo.getOperationType()==1) { // 完成任务 更新方案表实际卸载时间
            SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(manageEntityVo.getProcessInstanceId());
            SpArrangeSchemeDetail spDispatchSchemeDetail = new SpArrangeSchemeDetail();
            spDispatchSchemeDetail.setId(execute.getArrangeSchemeDetailId());
            spDispatchSchemeDetail.setRealityUnloadingDate(object.getUnloadingStartDate());
            spArrangeSchemeDetailService.updateById(spDispatchSchemeDetail);
            spDispatchSchemeDetailService.update(
                    new LambdaUpdateWrapper<SpDispatchSchemeDetail>()
                            .set(SpDispatchSchemeDetail::getRealityUnloadingDate,object.getUnloadingStartDate())
                            .eq(SpDispatchSchemeDetail::getArrangeSchemeDetailId,execute.getArrangeSchemeDetailId())
            );
            execute.setRealityUnloadingDate(object.getUnloadingStartDate());
            schemeDetailService.updateById(execute);
            object.setStatus(1);
            if(manageEntityVo.getTaskId()!=null){
                Map<String,Object> valible = new HashMap<>();
                valible.put("realityUnloadingDate", CalculateDateUtil.pasrseDate(object.getUnloadingStartDate()));
                taskService.addVaribals(valible,manageEntityVo.getTaskId());
            }
        }
        if(his==null){ // 新增
            object.setSchemeDetailId(manageEntityVo.getSchemeDetailId());
            object.setProceeInstanceId(manageEntityVo.getProcessInstanceId());
            object.setTaskId(manageEntityVo.getTaskId());
            mapperapper.insert(object);
        }else { // 修改
            object.setId(his.getId());
            //填充计量信息值
            if((his.getJlReport()==null && object.getJlReport()!=null) || (his.getJlReport()!=null && object.getJlReport()!=null && !his.getJlReport().equals(object.getJlReport()))){
                //SpFobSyzlxzwj reportObject = null;
                object.setField3("-1");
                asyncService.executeSyzlxzwj(his.getId(),object.getJlReport());
                /*try {
                    reportObject = relaxJLReport(object.getJlReport());
                } catch (IOException e) {
                    log.error("解析异常",e);
                }
                if(reportObject!=null){
                    try {
                        PropertiesUtil.copyProperties(reportObject,object);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }*/
            }

            BeanUtils.copyProperties(object,his);
            mapperapper.updateById(his);
            his = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlxzwj>().eq(SpFobSyzlxzwj::getProceeInstanceId,manageEntityVo.getProcessInstanceId()).ne(SpFobSyzlxzwj::getStatus,-2));

            // 更新卸载量字段

            SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(manageEntityVo.getProcessInstanceId());
            if(execute!=null){
                //
                execute.setRealityUnloadingDate(his.getUnloadingStartDate());
                SpArrangeSchemeDetail arrangeSchemeDetail = spArrangeSchemeDetailService.getById(execute.getArrangeSchemeDetailId());
                if(arrangeSchemeDetail!=null){
                    if(his.getWeight()!=null){
                        arrangeSchemeDetail.setRealityUnloadingCapacity(BigDecimal.valueOf(Float.valueOf(his.getWeight())));
                    }
                    arrangeSchemeDetail.setRealityUnloadingDate(his.getUnloadingStartDate());
                    spArrangeSchemeDetailService.updateById(arrangeSchemeDetail);
                    spDispatchSchemeDetailService.update(
                            new LambdaUpdateWrapper<SpDispatchSchemeDetail>()
                                    .set(SpDispatchSchemeDetail::getRealityUnloadingDate,his.getUnloadingStartDate())
                                    .eq(SpDispatchSchemeDetail::getArrangeSchemeDetailId,execute.getArrangeSchemeDetailId())
                    );
                }
                if(his.getWeight()!=null){
                    execute.setRealityUnloadingCapacity(BigDecimal.valueOf(Float.valueOf(his.getWeight())));
                }
                //execute.setContractUnloadingCapacity(BigDecimal.valueOf(Float.valueOf(his.getUnloadNum())));
                schemeDetailService.updateById(execute);
            }

            // 处理多余文件记录
            List<String> ids = new ArrayList<>();
            if((his.getFiels()!=null && his.getFiels().length()>0) || (his.getJlReport()!=null && his.getJlReport().length()>0)){
                String temp = his.getFiels();
                if(temp==null && his.getJlReport()!=null){
                    temp = his.getJlReport();
                }else if(his.getJlReport()!=null){
                    temp = temp+","+his.getJlReport();
                }
                String[] fieldIds = temp.split(",");
                ids =  Arrays.stream(fieldIds).collect(Collectors.toList());
            }
            if(his.getXzFile()!=null && his.getXzFile().length()>0){
                String temp = his.getXzFile();
                if(his.getXzFile()!=null){
                    temp = temp+","+his.getXzFile();
                }
                String[] fieldIds = temp.split(",");
                if(ids.size()==0){
                    ids =  Arrays.stream(fieldIds).collect(Collectors.toList());
                }else{
                    ids.addAll(Arrays.stream(fieldIds).collect(Collectors.toList()));
                }
            }
            if(his.getCommunicateEmail()!=null && his.getCommunicateEmail().length()>0){
                String temp = his.getCommunicateEmail();
                if(his.getXzFile()!=null){
                    temp = temp+","+his.getXzFile();
                }
                String[] fieldIds = temp.split(",");
                if(ids.size()==0){
                    ids =  Arrays.stream(fieldIds).collect(Collectors.toList());
                }else{
                    ids.addAll(Arrays.stream(fieldIds).collect(Collectors.toList()));
                }
            }
            spFobFileRecordService.deleteOtherFileRecor(his.getId(),ids);
        }
    }
    @Resource
    HttpRequestUtil httpRequestUtil;
    public SpFobSyzlxzwj relaxJLReport(String fileId) throws Exception {
        SpFobFileRecord fileRecord = spFobFileRecordService.getById(fileId);
        if(fileRecord!=null){
            URL url = new URL(fileRecord.getPath());
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            InputStream in = connection.getInputStream();
            if(in!=null){
                JSONObject infos = httpRequestUtil.ocrJLReport(in,fileId);
                SpFobSyzlxzwj spFobSyzlzzwj = JsonUtils.jsonToObject(JsonUtils.objectToJson(infos),SpFobSyzlxzwj.class);
                return spFobSyzlzzwj;
            }
        }
        return  null;
    }

    public static MultipartFile convert(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setConnectTimeout(5000);
        connection.setReadTimeout(5000);
        InputStream in = connection.getInputStream();

        byte[] body = new byte[in.available()];
        in.read(body);

        MultipartFile multipartFile = new MockMultipartFile("file", "file", "text/plain", body);

        return multipartFile;
    }

    @Resource
    SmThirdPartyMeasureService smThirdPartyMeasureService;

    @Resource
    SpFobQrxzgService spFobQrxzgService;
    // 完成任务执行同步信息
    public void createPayOrder(Long id) throws IOException {
        SpFobSyzlxzwj spFobSyzlzzwj = this.getById(id);
        syncThirdParty(spFobSyzlzzwj);
        // 完成同步信息
        createPushData(spFobSyzlzzwj);
    }



    @Autowired(required = false)
    ConThirdPartyMeasurementMapper conThirdPartyMeasurementMapper;
    @Autowired
    SpFobZdxzdsfjlService spFobZdxzdsfjlService;
    public Long syncThirdParty(SpFobSyzlxzwj spFobSyzlzzwj){
        SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(spFobSyzlzzwj.getProceeInstanceId());

        ThirdPartyMeasureBo thirdPartyMeasureBo = new ThirdPartyMeasureBo();
        thirdPartyMeasureBo.setDischargeStartDate(spFobSyzlzzwj.getUnloadingStartDate());
        thirdPartyMeasureBo.setDischargeEndDate(spFobSyzlzzwj.getUnloadingStartDate());
        thirdPartyMeasureBo.setVoyageType(1);
        SpFobQrxzg qrxzg = spFobQrxzgService.findData(spFobSyzlzzwj.getProceeInstanceId());
        if(qrxzg!=null && qrxzg.getExpectedUnloading()!=null){
            thirdPartyMeasureBo.setDischargePort(qrxzg.getExpectedUnloading());
        }
        if(execute!=null){
            thirdPartyMeasureBo.setVoyage(execute.getVoyageNo());
            if(thirdPartyMeasureBo.getDischargePort()==null){
                thirdPartyMeasureBo.setDischargePort(execute.getExpectedUnloadingPort());
            }
        }
        SpFobZdxzdsfjl spFobZdxzdsfjl = spFobZdxzdsfjlService.findData(spFobSyzlzzwj.getProceeInstanceId());
        if(spFobZdxzdsfjl!=null && spFobZdxzdsfjl.getCronId()!=null){
            ConThirdPartyMeasurement conThirdPartyMeasurement = conThirdPartyMeasurementMapper.selectById(spFobZdxzdsfjl.getCronId());
            if(conThirdPartyMeasurement!=null){
                thirdPartyMeasureBo.setDischargeIndependent(conThirdPartyMeasurement.getPartyB());
            }
        }
        //Gross Quantity
        thirdPartyMeasureBo.setDischargeGrossMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGrossQuantityMmbtu()));
        thirdPartyMeasureBo.setDischargeGrossTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGrossQuantityM3()));
        thirdPartyMeasureBo.setDischargeGrossMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGrossQuantityMt()));

        thirdPartyMeasureBo.setDischargeNetMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getNetQuantityMmbtu()));
        thirdPartyMeasureBo.setDischargeNetTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getNetQuantityM3()));
        thirdPartyMeasureBo.setDischargeNetMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getNetQuantityMt()));

        thirdPartyMeasureBo.setDischargeGhv(getStringToBigDecimal(spFobSyzlzzwj.getGhv()));
        thirdPartyMeasureBo.setDischargeTemperature(getStringToBigDecimal(spFobSyzlzzwj.getTemperature()));
        thirdPartyMeasureBo.setDischargeDensity(getStringToBigDecimal(spFobSyzlzzwj.getDensity()));
        thirdPartyMeasureBo.setDischargeGas(getStringToBigDecimal(spFobSyzlzzwj.getGasificationRate()));

        thirdPartyMeasureBo.setDischargeCoolMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getCoolDownMmbtu()));
        thirdPartyMeasureBo.setDischargeCoolTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getCoolDownM3()));
        thirdPartyMeasureBo.setDischargeCoolMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getCoolDownMt()));

        thirdPartyMeasureBo.setDischargeVapourMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getVapourMmbtu()));
        thirdPartyMeasureBo.setDischargeVapourMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getVapourMt()));
        thirdPartyMeasureBo.setDischargeVapourTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getVapourM3()));

        thirdPartyMeasureBo.setDischargeConsumedMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getBogConsumedMmbtu()));
        thirdPartyMeasureBo.setDischargeConsumedMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getBogConsumedMt()));
        thirdPartyMeasureBo.setDischargeConsumedTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getBogConsumedM3()));

        thirdPartyMeasureBo.setDischargeGassingMmbtuQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGassingUpMmbtu()));
        thirdPartyMeasureBo.setDischargeGassingMetersQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGassingUpMt()));
        thirdPartyMeasureBo.setDischargeGassingTonsQuantity(getStringToBigDecimal(spFobSyzlzzwj.getGassingUpM3()));

        if(spFobSyzlzzwj.getComponent()!=null && spFobSyzlzzwj.getComponent()!=""){
            try {
                //解析json数组
                List<JSONObject> list = JsonUtils.jsonToList(spFobSyzlzzwj.getComponent(),JSONObject.class);
                for(int i=0;i<list.size();i++){
                    JSONObject object = new JSONObject(list.get(i));
                    String name = object.getString("name");
                    String value = object.getString("value");
                    if(name.contains("Methane")){
                        thirdPartyMeasureBo.setDischargeMethane(getStringToBigDecimal(value));
                    }else if(name.contains("Ethane")){
                        thirdPartyMeasureBo.setDischargeEthane(getStringToBigDecimal(value));
                    }else if(name.contains("Propane")){
                        thirdPartyMeasureBo.setDischargePropane(getStringToBigDecimal(value));
                    } else if (name.contains("lso Butane")) {
                        thirdPartyMeasureBo.setDischargeIbutane(getStringToBigDecimal(value));
                    } else if (name.contains("N-Butane")) {
                        thirdPartyMeasureBo.setDischargeNbutane(getStringToBigDecimal(value));
                    } else if (name.contains("lso Pentane")) {
                        thirdPartyMeasureBo.setDischargeIpentane(getStringToBigDecimal(value));
                    } else if (name.contains("N-Pentane")) {
                        thirdPartyMeasureBo.setDischargeNpentane(getStringToBigDecimal(value));
                    }else if (name.contains("N-Hexane")){
                        thirdPartyMeasureBo.setDischargeNhexane(getStringToBigDecimal(value));
                    }else if(name.contains("Nitrogen")){
                        thirdPartyMeasureBo.setDischargeNitrogen(getStringToBigDecimal(value));
                    } else if (name.contains("Oxygen")) {
                        thirdPartyMeasureBo.setDischargeOxygen(getStringToBigDecimal(value));
                    }else if (name.contains("Carbon Dioxide")){
                        thirdPartyMeasureBo.setDischargeDioxide(getStringToBigDecimal(value));
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        SynchronThirdPartyMeasureVo vo = smThirdPartyMeasureService.saveSmThirdPartyMeasure(thirdPartyMeasureBo);
        if(spFobSyzlzzwj.getEvent()!=null && spFobSyzlzzwj.getEvent()!=""){
            try {
                List<JSONObject> list = JsonUtils.jsonToList(spFobSyzlzzwj.getEvent(),JSONObject.class);
                for(int i=0;i<list.size();i++){
                    JSONObject object = new JSONObject(list.get(i));
                    ThirdPartyMeasureRecordBo thirdPartyMeasureRecordBo = new ThirdPartyMeasureRecordBo();
                    thirdPartyMeasureRecordBo.setRecordContent(object.getString("event"));
                    if(object.get("date")!=null && object.getString("event")!=null){
                        thirdPartyMeasureRecordBo.setRecordTime(object.getString("date"));
                        thirdPartyMeasureRecordBo.setRecordType(1);
                        thirdPartyMeasureRecordBo.setThirdPartyMeasureId(vo.getId());
                        smThirdPartyMeasureService.saveSmThirdPartyMeasureRecord(thirdPartyMeasureRecordBo);
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
        //新增明细
        return vo.getId();
    }

    public BigDecimal getStringToBigDecimal(String num){
        if(StringUtils.isNotEmpty(num)){
            return new BigDecimal(num);
        }
        return null;
    }


    @Override
    public SpFobBaseBean getBaseBeanByInstanceId(String id) {
        return mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlxzwj>().eq(SpFobSyzlxzwj::getProceeInstanceId,id).ne(SpFobSyzlxzwj::getStatus,-2));
    }
    @Override
    public void  saveEntity(SpFobBaseBean bean){
        SpFobSyzlxzwj spFobZzgcjr = new SpFobSyzlxzwj();
        BeanUtils.copyProperties(bean,spFobZzgcjr);
        this.save(spFobZzgcjr);
    }

    @Override
    public void updateEntityById(SpFobBaseBean bean) {
        SpFobSyzlxzwj spFobZzgcjr = new SpFobSyzlxzwj();
        BeanUtils.copyProperties(bean,spFobZzgcjr);
        this.updateById(spFobZzgcjr);
    }


    @Override
    public Object getFormData(String id,Long serviceId) {
        SpFobSyzlxzwj spFobZzgcjr = null;
        if(serviceId!=null){
            spFobZzgcjr = mapperapper.selectById(serviceId);
        }else {
            spFobZzgcjr = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlxzwj>().eq(SpFobSyzlxzwj::getProceeInstanceId,id).ne(SpFobSyzlxzwj::getStatus,-2));
        }
        if(spFobZzgcjr==null){
            return null;
        }
        Map<String,Object> map = JsonUtils.jsonToMap(JsonUtils.objectToJson(spFobZzgcjr), Object.class);
        if(spFobZzgcjr.getJlReport()!=null){
            String[] ids = spFobZzgcjr.getJlReport().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordList =  spFobFileRecordService.listByIds(resId);
            map.put("jlReport",spFobFileRecordList);
        }
        if(StringUtils.isNotBlank(spFobZzgcjr.getFiels())){
            String[] ids = spFobZzgcjr.getFiels().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordLists =  spFobFileRecordService.listByIds(resId);
            map.put("fiels",spFobFileRecordLists);
        }
        if(spFobZzgcjr.getXzFile()!=null){
            String[] ids = spFobZzgcjr.getXzFile().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordLists =  spFobFileRecordService.listByIds(resId);
            map.put("xzFile",spFobFileRecordLists);
        }
        if(spFobZzgcjr.getCommunicateEmail()!=null){
            String[] ids = spFobZzgcjr.getCommunicateEmail().split(",");
            List<Long> resId = new ArrayList<>();
            for(String tempId:ids){
                if (StringUtils.isNotBlank(tempId)) {
                    resId.add(Long.parseLong(tempId));
                }
            }
            List<SpFobFileRecord> spFobFileRecordLists =  spFobFileRecordService.listByIds(resId);
            map.put("communicateEmail",spFobFileRecordLists);
        }
        if(!map.containsKey("component") || map.get("component")==null){
            //初始化component数据
            JSONArray jsonArray = new JSONArray();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name","Methane (CH4)");
            jsonObject.put("value","");
            jsonObject.put("unit","%Mol");
            jsonArray.add(jsonObject);
            JSONObject jsonObject1 = new JSONObject();
            jsonObject1.put("name","Ethane (C2H6)");
            jsonObject1.put("value","");
            jsonObject1.put("unit","%Mol");
            jsonArray.add(jsonObject1);
            JSONObject jsonObject2 = new JSONObject();
            jsonObject2.put("name","Propane (C3H8)");
            jsonObject2.put("value","");
            jsonObject2.put("unit","%Mol");
            jsonArray.add(jsonObject2);
            JSONObject jsonObject3 = new JSONObject();
            jsonObject3.put("name","lso Butane (i-C4H10)");
            jsonObject3.put("value","");
            jsonObject3.put("unit","%Mol");
            jsonArray.add(jsonObject3);
            JSONObject jsonObject4 = new JSONObject();
            jsonObject4.put("name","N-Butane(n-C4H10)");
            jsonObject4.put("value","");
            jsonObject4.put("unit","%Mol");
            jsonArray.add(jsonObject4);
            JSONObject jsonObject5 = new JSONObject();
            jsonObject5.put("name","lso Pentane (i-C5H12)");
            jsonObject5.put("value","");
            jsonObject5.put("unit","%Mol");
            jsonArray.add(jsonObject5);
            JSONObject jsonObject6 = new JSONObject();
            jsonObject6.put("name"," N-Pentane (n-C5H12)");
            jsonObject6.put("value","");
            jsonObject6.put("unit","%Mol");
            jsonArray.add(jsonObject6);
            JSONObject jsonObject7 = new JSONObject();
            jsonObject7.put("name"," N-Hexane (n-C6H14)");
            jsonObject7.put("value","");
            jsonObject7.put("unit","%Mol");
            jsonArray.add(jsonObject7);
            JSONObject jsonObject8 = new JSONObject();
            jsonObject8.put("name"," Nitrogen (N2)");
            jsonObject8.put("value","");
            jsonObject8.put("unit","%Mol");
            jsonArray.add(jsonObject8);
            JSONObject jsonObject9 = new JSONObject();
            jsonObject9.put("name","Oxygen (02)");
            jsonObject9.put("value","");
            jsonObject9.put("unit","%Mol");
            jsonArray.add(jsonObject9);
            JSONObject jsonObject10 = new JSONObject();
            jsonObject10.put("name","Carbon Dioxide (C02)");
            jsonObject10.put("value","");
            jsonObject10.put("unit","%Mol");
            jsonArray.add(jsonObject10);
            map.put("component",JsonUtils.objectToJson(jsonArray));
        }
        if(!map.containsKey("event") || map.get("event")==null) {
            //初始化component数据
            JSONArray jsonArray = new JSONArray();
            map.put("event",JsonUtils.objectToJson(jsonArray));
        }
        SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(spFobZzgcjr.getProceeInstanceId());
        if (execute.getExpectedUnloadingPort()!=null && spFobZzgcjr.getPort() == null){
            map.put("port",execute.getExpectedUnloadingPort());
        }
        return map;
    }


    @Override
    public SpFobSyzlxzwj findData(String processInstanceId) {
        SpFobSyzlxzwj spFobZzgcjr = mapperapper.selectOne(new LambdaQueryWrapper<SpFobSyzlxzwj>().eq(SpFobSyzlxzwj::getProceeInstanceId,processInstanceId).ne(SpFobSyzlxzwj::getStatus,-2));
        return spFobZzgcjr;
    }

    @Override
    public List<Long> findServieData(String processInstanceId){
        List<SpFobSyzlxzwj> spFobBaseBeans =  mapperapper.selectList(new LambdaQueryWrapper<SpFobSyzlxzwj>().eq(SpFobSyzlxzwj::getProceeInstanceId,processInstanceId).orderByDesc(SpFobSyzlxzwj::getCreateDate));
        List<Long> list = spFobBaseBeans.stream().map(SpFobBaseBean::getId).collect(Collectors.toList());
        return list;
    }


    @Autowired
    ShipDashboarDataService shipDashboarDataService;


    @Autowired
    ConShippingService conShippingService;
    /**
     * 完成任务推送信息
     * @param id
     */

    @Autowired
    SpFobSyzlzzwjService spFobSyzlzzwjService;

    @Autowired
    SpFobGczfstdService spFobGczfstdService;
    public void createPushData(SpFobSyzlxzwj  spFobSyzlxzwj) throws IOException {
        SpDispatchSchemeExecute execute = schemeDetailService.getExceuteByInstanceId(spFobSyzlxzwj.getProceeInstanceId());
        ShipDashboarDataRq shipDashboarDataRq = new ShipDashboarDataRq();
        shipDashboarDataRq.setBuyer(execute.getBuyer());
        shipDashboarDataRq.setVoyageNo(execute.getVoyageNo());
        shipDashboarDataRq.setGoodsNum(execute.getGoodsNum());
        shipDashboarDataRq.setSeller(execute.getSeller());
        shipDashboarDataRq.setTradeType(execute.getTradeType()+"");
        shipDashboarDataRq.setBuyer(execute.getBuyer());
        shipDashboarDataRq.setShipName(execute.getShipName());
        shipDashboarDataRq.setImo(execute.getImo());
        shipDashboarDataRq.setShipTradeBody(execute.getShipTradeBody()+"");
        shipDashboarDataRq.setShipContractType(execute.getShipContractType()+"");
        shipDashboarDataRq.setExpectedLoadingPort(execute.getExpectedLoadingPort());
        shipDashboarDataRq.setLoadingCapacity(execute.getLoadingCapacity());
        shipDashboarDataRq.setRealityLoadingCapacity(execute.getRealityLoadingCapacity());
        shipDashboarDataRq.setExpectedLoadingDate(execute.getExpectedLoadingDate());
        shipDashboarDataRq.setExpectedLoadingCompletionWindow(execute.getExpectedLoadingCompletionWindow());
        shipDashboarDataRq.setExpectedFullLoadVoyageDays(execute.getExpectedFullLoadVoyageDays());
        SpFobSyzlzzwj spFobSyzlzzwj = spFobSyzlzzwjService.findActivitiData(spFobSyzlxzwj.getProceeInstanceId());
        if(spFobSyzlzzwj!=null){
            shipDashboarDataRq.setRealityLoadingDate(spFobSyzlzzwj.getLoadingStartDate());
            shipDashboarDataRq.setLoadingEndDate(spFobSyzlzzwj.getLoadingEndDate());
        }
        SpFobQrxzg spFobQrxzg = spFobQrxzgService.findData(spFobSyzlxzwj.getProceeInstanceId());
        if(spFobQrxzg!=null){
            shipDashboarDataRq.setExpectedUnloadingPort(spFobQrxzg.getExpectedUnloading());
        }
        shipDashboarDataRq.setContractUnloadingCapacity(execute.getContractUnloadingCapacity());
        shipDashboarDataRq.setRealityUnloadingCapacity(execute.getRealityUnloadingCapacity());
        shipDashboarDataRq.setExpectedUnloadingWindow(execute.getExpectedUnloadingWindow());
        shipDashboarDataRq.setExpectedUnloadingCompletionWindow(execute.getExpectedUnloadingCompletionWindow());
        //shipDashboarDataRq.setRealityUnloadingDate(execute.getRealityUnloadingDate());
        if(spFobSyzlxzwj!=null){
            shipDashboarDataRq.setUnloadingEndDate(spFobSyzlxzwj.getUnloadingStartDate());
            shipDashboarDataRq.setRealityUnloadingDate(spFobSyzlxzwj.getUnloadingStartDate());
        }
        shipDashboarDataRq.setExpectedEmptyLoadVoyageDays(execute.getExpectedEmptyLoadVoyageDays());
        shipDashboarDataRq.setExpectedEmptyReturnWindow(execute.getExpectedEmptyReturnWindow());
        SpFobGczfstd spFobGczfstd = spFobGczfstdService.findDate(spFobSyzlxzwj.getProceeInstanceId());
        if(spFobGczfstd!=null){
            shipDashboarDataRq.setTdNo(spFobGczfstd.getTdNo());
        }
        shipDashboarDataService.pushGuanWuData(shipDashboarDataRq);
    }

}
